package edu.nf.project.webs.storeOperation;

import edu.nf.project.entitys.dto.storeOperation.ConfirmOrderRequest;
import edu.nf.project.entitys.dto.storeOperation.SubmitOrderRequest;
import edu.nf.project.entitys.dto.storeOperation.SubmitOrderResult;
import edu.nf.project.services.exception.derivedAnomalies.DishException;
import edu.nf.project.services.other.InventoryService;
import edu.nf.project.services.response.BaseController;
import edu.nf.project.services.response.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author gjh
 * @Date 2025/6/20
 * @Description:
 */
@RestController
@RequestMapping("/api/order")
public class OrderController extends BaseController {

    @Autowired
    private InventoryService inventoryService;

    @PostMapping("/submit")
    public ResultVO<SubmitOrderResult> submitOrder(@RequestBody SubmitOrderRequest request) {
        try{
            SubmitOrderResult result = inventoryService.checkAndLockInventory(request);
            return success(result);
        }catch(Exception e) {
            if (e instanceof DishException) {
                throw e;
            }
            System.out.println(e);
            throw new DishException(40000, "系统错误，请稍后重试");

        }
    }


    @PostMapping("/confirm")
    public ResultVO<Void> confirmInventory(@RequestBody ConfirmOrderRequest request) {
        inventoryService.confirmInventory(request.getOrderId());
        return success();
    }

}

